﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBSourceToolsLib;
using DBSourceToolsLib.Composite;

namespace DBSourceTools
{
    public class NewQueryHelper
    {
        // TODO : COMPLETE 1.2.0.8 right click on queries folder to create new query. (billhuber)

        public static void NewQuery(DBSourceToolsLib.Commands.DatabaseCommandTypes queryType, Form oParent, string strParams = null)
        {
            oParent.Cursor = Cursors.WaitCursor;
            try
            {
                SourceDatabase oSourceDb = UserSettings.Instance().getSourceDatabase(
                        UserSettings.Instance().CurrentDbNickName
                    );

                if (oSourceDb != null)
                {
                    DBSourceToolsLib.SourceDatabaseLoader oLoader = new SourceDatabaseLoader(
                        oParent, queryType);

                    oLoader.Execute(ref oSourceDb, strParams);

                    WindowList.Instance().ShowWindow(new TreeNodeTag(oSourceDb.NickName, "File", oLoader.NewQueryFileName, strParams));
                    // update treeview
                }
                
            }
            catch (Exception ex)
            {
                DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm oError =
                    new DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm(ex.ToString());
                oError.ShowDialog(oParent);
            }
            oParent.Cursor = Cursors.Default;
        }

        public static void NewPatch(Form oParent, TreeNodeTag oTag)
        {
            oParent.Cursor = Cursors.WaitCursor;
            try
            {
                SourceDatabase oSourceDb = UserSettings.Instance().getSourceDatabase(
                        UserSettings.Instance().CurrentDbNickName
                    );

                RenameForm oNewName = new RenameForm("New PatchName Name");
                oNewName.SetFileName("Patch_");
                if (oNewName.ShowDialog(oParent) == DialogResult.OK)
                {
                    string strFileName = oNewName.NewName;
                    if (!strFileName.ToLower().Contains(".sql"))
                        strFileName += ".sql";

                    TargetDatabase oTargetDb = (TargetDatabase)UserSettings.Instance().DatabaseProject.GetDatabaseEx(oTag.ObjectName);
                    
                    if (oTargetDb != null)
                    {
                        string strFullFileName = oTargetDb.NewPatch(strFileName);
                        WindowList.Instance().ShowWindow(new TreeNodeTag(oSourceDb.NickName, "File", strFullFileName, null));
                    }

                }


            }
            catch (Exception ex)
            {
                DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm oError =
                    new DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm(ex.ToString());
                oError.ShowDialog(oParent);
            }
            oParent.Cursor = Cursors.Default;

        }
    }
}
